import Vue from "vue";
import {getUserInfo} from "@/utils/lac";

/**
 * Action 权限指令
 * 指令用法：
 *  - 在需要控制 action 级别权限的组件上使用 v-action:[method] , 如下：
 *    <i-button v-action:add >添加用户</a-button>
 *    <a-button v-action:delete>删除用户</a-button>
 *    <a v-action:edit @click="edit(record)">修改</a>
 *
 *  - 当前用户没有权限时，组件上使用了该指令则会被隐藏
 *  - 当后台权限跟 pro 提供的模式不同时，只需要针对这里的权限过滤进行修改即可
 *
 *  @see https://github.com/vueComponent/ant-design-vue-pro/pull/53
 */
function formatArray(arr) {
  let list = [];
  if (arr.length) {
    for (let i in arr) {
      if (arr[i].childList.length) {
        delChildren(arr[i].childList);
      } else {
        list.push(arr[i]);
      }
    }
  }
  return list;
}

let aa;

function arrayItem(item, arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].webUrl != item) {
      if (arr[i].childList.length) {
        arrayItem(item, arr[i].childList);
      }
    }
    if (arr[i].webUrl == item) {
      aa = arr[i];
    }
  }
}

/*const action = Vue.directive('action', {
  inserted: function (el, binding, vnode) {
    const actionName = binding.arg
    const roles = store.getters.roles
    const elVal = vnode.context.$route.meta.permission
    const permissionId = elVal instanceof String && [elVal] || elVal
    if (roles.length > 0) {
      arrayItem(router.app.$route.path, roles)
    }
    
    let ite = aa
    if (ite&&ite.childList.length > 0) {
      ite.authoritys = []
      ite.childList.map((item) => {
        ite.authoritys.push(item.authority)
      })
    }

    if (ite&&ite.authoritys == undefined) {
      el.parentNode && el.parentNode.removeChild(el) || (el.style.display = 'none')
    }
    if (ite&&ite.authoritys && !ite.authoritys.includes(actionName)) {
      el.parentNode && el.parentNode.removeChild(el) || (el.style.display = 'none')
    }
  }
})*/

const action = Vue.directive('action', {
  inserted: function (el, binding, vnode) {
    const actionName = binding.arg
    const roles =getUserInfo().menuList
    roles.forEach((item)=>{
      // console.log(item.webUrl ===vnode.context.$route.path,item.webUrl ,vnode.context.$route.path,item.authoritys)
      if(item.webUrl ===vnode.context.$route.path){
        if (item.authority.includes(actionName)) {
          return;
        }
        if (!item.authority.includes(actionName)) {
          (el.parentNode && el.parentNode.removeChild(el)) ||
          (el.style.display = "none");
        }
      }
    })



  }
})

export default action;
